//
//  HBTaskItemCell.m
//  HeartBeat
//
//  Created by karong on 2023/8/29.
//

#import "HBTaskItemCell.h"
#import "HBTaskModel.h"

@interface HBTaskItemCell ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *receiveBtn;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *minTitleLabel;
@end

@implementation HBTaskItemCell
- (void)setDataWithObject:(HBTaskModel *)object {
    self.titleLabel.text = object.title;
    self.subTitleLabel.text = SF(@"+%ld%@",  (long)object.coins, WSLocalized(@"書幣"));
    self.descLabel.text = object.desc;
    [self setMinTitleLabelTextWithObject:object];
    [self setReceiveBtnStatusWithObject:object];
}

- (void)setMinTitleLabelTextWithObject:(HBTaskModel *)object {
    BOOL show = NO;
    NSString * text = @"";
    if (object.is_show_progress == 1 || object.times > 1 || object.task_identify == 8 || object.task_identify == 9 || object.task_identify == 11) {
        show = YES;
    }
    if (show) {
        if (object.total_times > 0) {
            text = SF(@"(%ld/%ld%@)", object.over_times, object.total_times, object.unit_name);
        }
        else if (object.task_identify == 9) {
            text = SF(@"(%ld/%ld%@)", object.over_times, object.unit_num, object.unit_name);
        }
        else {
            text = SF(@"(%ld/%ld)", object.over_times, object.total_times);
        }
        self.minTitleLabel.text = text;
    }
    self.minTitleLabel.hidden = !show;
}

- (void)setReceiveBtnStatusWithObject:(HBTaskModel *)object {
    ///任務狀態（0未完成，1待領取，2已領取）
    UIColor * textColor = UIColor.clearColor;
    UIColor * borderColor = UIColor.clearColor;
    UIColor * bgColor = UIColor.clearColor;
    NSString * text = @"";
    BOOL enable = YES;
    switch (object.task_status) {
        case 0:
            textColor = UIColor.font_yellow_FFCC00;
            borderColor = UIColor.font_yellow_FFCC00;
            text = WSLocalized(@"去完成");
            break;
        case 1:
            textColor = UIColor.font_black_1A1A1A;
            bgColor = UIColor.font_yellow_FFCC00;
            text = WSLocalized(@"領取");
            break;
        case 2:
            textColor = UIColor.font_gray_999999;
            bgColor = UIColor.bg_gray_f2f2f2;
            text = WSLocalized(@"已完成");
            enable = NO;
            break;
        default:
            break;
    }
    if (object.task_identify == 0) {
        enable = YES;
    }
    self.receiveBtn.tag = object.jump_type;
    self.receiveBtn.userInteractionEnabled = enable;
    self.receiveBtn.layer.borderColor = borderColor.CGColor;
    [self.receiveBtn setTitleColor:textColor forState:(UIControlStateNormal)];
    [self.receiveBtn setBackgroundColor:bgColor];
    [self.receiveBtn setTitle:text forState:(UIControlStateNormal)];
    
}

- (IBAction)receiveAction:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(taskItemCellAction:type:)]) {
        [self.delegate taskItemCellAction:self type:sender.tag];
    }
}
@end
